package com.time.compent;

import com.time.domain.User;
import com.time.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author 张龙佼
 * @Description 启动项目时，若没有管理员，则创建管理员
 * @since 2020-10-14
 */

@Component
public class CreateDefaultAdmin implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    private UserService userService;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        User userExist = userService.getById("admin");

        if (userExist == null) {
            User user = new User();
            user.setUserId("admin");
            user.setUserName("管理员");
            user.setPassword("admin");
            user.setSex("T");
            user.setBirthday(new Date());
            user.setIdentityCode("431228199711092645");
            user.setEmail("27298347392@qq.com");
            user.setMobile("13523149870");
            user.setAddress("北京海淀区");
            user.setStatus(2);

            userService.addUser(user);
        }
    }
}
